package deng.jboss.examples.jpawebservices;

import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;

/** 
 * 
 * Note that this WS class can NOT be used in as servlet in web.xml, or else
 * your EM will be null. You must deploy this as .ejb or .jar file since this
 * is an EJB now.
 * 
<pre>
Sample of SOAP request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:jpaw="http://jpawebservices.examples.jboss.deng/">
   <soapenv:Header/>
   <soapenv:Body>
      <jpaw:addSubject3>
         <subject>
            <firstName>Zemian</firstName>
            <lastName>Deng</lastName>
            <dateOfBirth>01/01/2010</dateOfBirth>
            <image></image>
            <imageType></imageType>
         </subject>
      </jpaw:addSubject3>
   </soapenv:Body>
</soapenv:Envelope>
</pre> 
 * 
 * @author dengz1
 *
 */
@Stateless
@WebService
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT)
public class Subject3DALImpl implements Subject3DAL {
	
	private static Logger logger = Logger.getLogger(Subject3DALImpl.class);
	
	@PersistenceContext(unitName="JBOSSDB")
	private EntityManager em;
	
	@WebMethod
	public Subject3 getSubject3(@WebParam(name="subjectId") Long id) {
		logger.debug("Retrieving subject ID: " + id);
		Subject3 subject = em.getReference(Subject3.class, id);
		logger.info("Found subject: " + subject);
		return subject;
	}
	
	@WebMethod
	public List<Subject3> getSubject3List() {
		logger.debug("Retrieving all subject list.");
		@SuppressWarnings("unchecked") // The cast is safe to ignore. JPA deficiency.
		List<Subject3> subjectList = em.createQuery("select s from Subject3 as s").getResultList();
		logger.info(subjectList.size() + " records found.");
		return subjectList;
	}
	
	@WebMethod
	public Long addSubject3(@WebParam(name="subject") Subject3 subject) {
		logger.debug("Adding new subject: " + subject);
		em.persist(subject);
		logger.info("New subject saved with ID: " + subject.getId());
		return subject.getId();
	}
	
	@WebMethod
	public boolean exists(@WebParam(name="subjectId") Long id) {
		logger.debug("Checking subject exist for ID: " + id);
		Subject3 subject = em.find(Subject3.class, id);
		boolean found = (subject != null);
		logger.info("Subject ID: " + id + (found ? " exists" : " doesn't exists."));
		return found;
	}
	
	@WebMethod
	public Subject3 deleteSubject3(@WebParam(name="subjectId") Long id) {
		logger.debug("Deleting subject ID: " + id);
		Subject3 subject = getSubject3(id);
		em.remove(subject);
		logger.info("Subject removed.");
		return subject;
	}
	
	@WebMethod
	public void updateSubject3(@WebParam(name="subject") Subject3 subject) {
		logger.debug("Updating subjectID: " + subject.getId());
		getSubject3(subject.getId()); // ensure subject exists.
		em.merge(subject);
		logger.info("Subject has been updated with: " + subject);
	}
	
}
